<?php

namespace Kunfeng\Dify\Model;

/**
 * Dify 客户端配置
 */
class DifyConfig
{
    private $baseUrl;
    private $apiKey;
    private $connectTimeout;
    private $readTimeout;
    private $writeTimeout;
    private $httpOptions;

    public function __construct(
        string $baseUrl,
        string $apiKey,
        int $connectTimeout = 5000,
        int $readTimeout = 60000,
        int $writeTimeout = 30000,
        ?array $httpOptions = null
    ) {
        $this->baseUrl = $baseUrl;
        $this->apiKey = $apiKey;
        $this->connectTimeout = $connectTimeout;
        $this->readTimeout = $readTimeout;
        $this->writeTimeout = $writeTimeout;
        $this->httpOptions = $httpOptions;
    }

    /**
     * 从数组创建配置
     */
    public static function fromArray(array $config): self
    {
        if (!isset($config['base_url'])) {
            throw new \InvalidArgumentException('base_url is required');
        }
        if (!isset($config['api_key'])) {
            throw new \InvalidArgumentException('api_key is required');
        }

        return new self(
            $config['base_url'],
            $config['api_key'],
            $config['connect_timeout'] ?? 5000,
            $config['read_timeout'] ?? 60000,
            $config['write_timeout'] ?? 30000,
            $config['http_options'] ?? null
        );
    }

    /**
     * 创建配置构建器
     */
    public static function builder(): DifyConfigBuilder
    {
        return new DifyConfigBuilder();
    }

    /**
     * 转换为数组
     */
    public function toArray(): array
    {
        return [
            'base_url' => $this->baseUrl,
            'api_key' => $this->apiKey,
            'connect_timeout' => $this->connectTimeout,
            'read_timeout' => $this->readTimeout,
            'write_timeout' => $this->writeTimeout,
            'http_options' => $this->httpOptions,
        ];
    }

    public function getBaseUrl(): string
    {
        return $this->baseUrl;
    }

    public function getApiKey(): string
    {
        return $this->apiKey;
    }

    public function getConnectTimeout(): int
    {
        return $this->connectTimeout;
    }

    public function getReadTimeout(): int
    {
        return $this->readTimeout;
    }

    public function getWriteTimeout(): int
    {
        return $this->writeTimeout;
    }

    public function getHttpOptions(): ?array
    {
        return $this->httpOptions;
    }
}

/**
 * 配置构建器
 */
class DifyConfigBuilder
{
    private $baseUrl;
    private $apiKey;
    private $connectTimeout = 5000;
    private $readTimeout = 60000;
    private $writeTimeout = 30000;
    private $httpOptions = null;

    public function baseUrl(string $baseUrl): self
    {
        $this->baseUrl = $baseUrl;
        return $this;
    }

    public function apiKey(string $apiKey): self
    {
        $this->apiKey = $apiKey;
        return $this;
    }

    public function connectTimeout(int $timeout): self
    {
        $this->connectTimeout = $timeout;
        return $this;
    }

    public function readTimeout(int $timeout): self
    {
        $this->readTimeout = $timeout;
        return $this;
    }

    public function writeTimeout(int $timeout): self
    {
        $this->writeTimeout = $timeout;
        return $this;
    }

    public function httpOptions(array $options): self
    {
        $this->httpOptions = $options;
        return $this;
    }

    public function build(): DifyConfig
    {
        if (!isset($this->baseUrl)) {
            throw new \InvalidArgumentException('baseUrl is required');
        }
        if (!isset($this->apiKey)) {
            throw new \InvalidArgumentException('apiKey is required');
        }

        return new DifyConfig(
            $this->baseUrl,
            $this->apiKey,
            $this->connectTimeout,
            $this->readTimeout,
            $this->writeTimeout,
            $this->httpOptions
        );
    }
}